<template>
  <div class="home">
    <div class="es">
      <div class="es-dic">
        <el-button circle icon="el-icon-circle-plus-outline" type="primary" title="新增" size="small"
                   @click="createAdd" style="margin: 10px;"></el-button>
        <div>
          <span style="font-weight: bold; color: red;">新增热词：已存在的es中的数据不生效，之后添加的es数据才生效</span>
        </div>
        <!--分页-->
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!--表格内容-->
        <el-table
          ref="list"
          :data="tableData"
          style="width: 100%"
          border
          stripe
          highlight-current-row
          :default-sort="{prop: 'date', order: 'descending'}"
          @row-click="handleRowClick"
          @select-all="handleCheckedAllAndCheckedNone"
          @select="handleCheckedAllAndCheckedNone">
          <el-table-column
            type="selection"
            width="45"
            align="center">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column label="名称">
            <template slot-scope="scope">
              {{tableData[scope.$index]}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="130"
            align="center">
            <template slot-scope="scope">
              <el-button circle icon="el-icon-delete" type="danger" title="删除" size="small"
                         @click="rowDel(scope.$index, scope.row, $event)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--编辑-弹出层-->
        <el-dialog
          title="新增"
          :visible.sync="isShowEditDialog"
          width="430px"
          @close="dialogClose">
          <el-form
            ref="editForm"
            :model="formFileds"
            label-width="55px"
            :rules="rules">
            <el-form-item label="名称" prop="name">
              <el-input v-model="formFileds.name"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleCreate(formFileds.name)" class="pull-right margin-l-25">确定
              </el-button>
              <el-button @click="isShowEditDialog = false" class="pull-right">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Dic",
    data() {
      return {
        formFileds: {
          name: ''
        },
        rules: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur, change'}
          ]
        },
        tableData: [],
        isShowEditDialog: false,
        total: 0
      }
    },
    methods: {
      handleRowClick(row, event, column) {
        // 仅选中当前行
        this.setCurRowChecked(row);
      },
      handleCheckedAllAndCheckedNone(selection) {
        // 当前选中仅一行时操作-（当前表格行高亮）
        1 != selection.length && this.$refs.list.setCurrentRow();
      },
      dialogClose() {
        // 清空编辑表单
        this.$refs.editForm.resetFields();
      },
      createAdd() {
        this.setCurRowChecked();
        this.isShowEditDialog = true;
      },
      handleCreate(name) {
        this.$refs.editForm.validate(isValid => {
          if (!isValid) return;
          this.$axios.get('/dic/fileWrite', {
            params: {
              value: name
            }
          }).then(res => {
            if (res.data.code === 2000) {
              this.tableData = res.data.result;
              this.isShowEditDialog = false;
              this.$message.success('新增成功');
            } else {
              this.$message.error(res.data.result);
            }
          })
        });
      },
      rowDel(index, row, event) {
        console.log(index)
        console.log('row:', row)
        console.log(event)
        // 让当前删除按钮失焦
        event.target.blur();
        this.$confirm('确定要删除当前行吗？', '删除', {
          comfirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          this.$axios.get('/dic/fileDelete', {
            params: {
              value: row
            }
          }).then(res => {
            this.tableData.splice(index, 1);
            this.$message.success('删除成功');
          })
          return false;
        });
      },
      // 选中当前行-当前行的复选框被勾选
      setCurRowChecked(row) {
        this.$refs.list.clearSelection();
        this.$refs.list.toggleRowSelection(row);
      },
      searchDic() {
        this.$axios.get('/dic/fileReader').then(res => {
          this.tableData = res.data.result;
          this.total = res.data.total;
        })
      }
    },
    activated() {
      this.searchDic();
    }
  }
</script>

<style scoped lang="less">
  .home {
    height: calc(~'100% - 40px');
  }

  .es {
    display: flex;
    height: 100%;
    width: 100%;
  }

  .es-dic {
    position: relative;
    width: 100%;
    background-color: @boxBgColor;
    box-shadow: 2px 2px 5px #ccc;
    overflow-y: auto;
  }

  .el-form {
    padding: 0 10px;
  }

  .el-date-editor {
    width: 100% !important;
  }
</style>
